<?php


namespace app\admin\controller;


use app\admin\validate\RoomPasswordValidate;
use app\BaseController;
use app\model\RoomPassword;
use app\service\RoomPasswordService;
use think\App;

class RoomPasswordController extends BaseController
{
    private $RoomPasswordService;

    public function __construct(App $app)
    {
        $this->RoomPasswordService = new RoomPasswordService();
        parent::__construct($app);
    }

    public function getRoomPassword(RoomPassword $roomPassword){
        return $this->returnJson($this->RoomPasswordService->getRoomPassword($roomPassword->id));
    }

    public function getRoomPasswordList(){
        $param = $this->request->param(['page' => 1 , 'limit' => 10]);
        return $this->returnJson(
            $this->RoomPasswordService->getRoomPasswordList($param['page'] , $param['limit']));
    }

    public function editRoomPassword(RoomPassword $roomPassword){
        $id = $roomPassword->id;
        $param = $this->request->param(['password' => '' , 'start_time' => '' , 'end_time' => '']);
        return $this->returnJson($this->RoomPasswordService->editRoomPasswordByRoomId($id , $param));
    }

    public function delRoomPassword(RoomPassword $roomPassword){
        $this->RoomPasswordService->delRoomPassword($roomPassword->id);
        return $this->returnJson();
    }

    public function addRoomPassword(){
        $param = $this->request->param(['room_id' ,'password' , 'start_time' , 'end_time']);
        validate(RoomPasswordValidate::class)
            ->scene('addRoomPassword')
            ->check($param);
        return $this->returnJson($this->RoomPasswordService->addRoomPassword(
            $param['room_id'],
            $param['password'],
            $param['start_time'],
            $param['end_time']
        ));
    }
}